#!/bin/bash
# This file gets sourced from all vhost-* scripts.

vhost_configure() {
	chattr -i $VHOST_FCGIFILE_PATH

	# Remove all Comments and empty lines
	sed -e "/^;/d" $VHOST_PHPINI_MASTERFILE_PATH | sed  "/^$/d" > $VHOST_PHPINIFILE_PATH

	# Insert some Settings after [PHP] which are commented by default. Only to be sure sed finds this setting on the next line and can substitute this settings.
	sed -i "/\[PHP\]/{n;s/.*/error_log = \\ncgi.fix_pathinfo = \\nopen_basedir = \\nerror_reporting = \\nlog_errors = \\ndoc_root = \\ninclude_path = /;}" $VHOST_PHPINIFILE_PATH

	# Substitute original with our modified settings.
	sed -i "/^error_reporting[ ]*=.*/s.*error_reporting=E_ALL \& E_STRICTg
	/^log_errors[ ]*=.*/s.*log_errors=Ong
	/^error_log[ ]*=.*/s.*error_log=${VHOST_LOG_PATH}/php-error.logg
	/^doc_root[ ]*=.*/s.*doc_root=$VHOST_PUBLIC_PATHg
	/^cgi.fix_pathinfo[ ]*=.*/s.*cgi.fix_pathinfo=1g
	/^open_basedir[ ]*=.*/s.*open_basedir=${VHOST_DIR}g
	/^include_path[ ]*=.*/s.*include_path=\".:/usr/share/php5:usr/share/php\"g" $VHOST_PHPINIFILE_PATH

	# Delete all duplicated lines in the file.
	sed -ni 'G; s/\n/&&/; /^\([ -~]*\n\).*\n\1/d; s/\n//; h; P' $VHOST_PHPINIFILE_PATH
	
	sed -i "s/#VHOST#/${VHOST}/g
	s/#USER#/${USER}/g
	s/#GROUP#/${GROUP}/g
	s/#REMAINING_DOMAIN#/${REMAINING_DOMAIN}/g
	s#VHOSTS_DIR#${VHOSTS_DIR}g
	s#VHOST_DIR#${VHOST_DIR}g
	s/#DOMAIN#/${DOMAIN}/g
	s#VHOST_CFG_PATH#$VHOST_CFG_PATHg
	s#VHOST_BIN_PATH#$VHOST_BIN_PATHg
	s#VHOST_WWW_PATH#$VHOST_WWW_PATHg
	s#VHOST_LOG_PATH#$VHOST_LOG_PATHg
	s#VHOST_PUBLIC_PATH#$VHOST_PUBLIC_PATHg
	s#VHOST_FCGIFILE_PATH#$VHOST_FCGIFILE_PATHg
	s#VHOST_PHPRC_PATH#$VHOST_PHPRC_PATHg
	s#VHOST_PHPINIFILE_PATH#$VHOST_PHPINIFILE_PATHg
	s#VHOST_CONFIGFILE_PATH#$VHOST_CONFIGFILE_PATHg
	s#USER_HOME_PATH#$USER_HOME_PATHg
	s#USER_BIN_PATH#$USER_BIN_PATHg" $VHOST_CONFIGFILE_PATH $VHOST_FCGIFILE_PATH $VHOST_PHPINIFILE_PATH
	
	chown $USER:$GROUP -R $VHOST_DIR
	chown root:root -R $VHOST_CFG_PATH/*
	chmod 644 $VHOST_CONFIGFILE_PATH
	chmod 644 $VHOST_PHPINIFILE_PATH
	chmod 500 $VHOST_FCGIFILE_PATH
	chown ${USER}:${GROUP} $VHOST_FCGIFILE_PATH
	chattr +i $VHOST_FCGIFILE_PATH
}

_prepare_vars() {
        REMAINING_DOMAIN=`echo $REMAINING_DOMAIN|sed "s/#USER#/${USER}/g
        s/#GROUP#/${GROUP}/g"`

        DOMAIN=`echo $DOMAIN|sed "s/#USER#/${USER}/g
        s/#GROUP#/${GROUP}/g
        s/#REMAINING_DOMAIN#/${REMAINING_DOMAIN}/g
        s/#VHOST#/${VHOST}/g"`

	USER_HOME_PATH=`echo $USER_HOME_PATH|sed "s#USER#${USER}g
	s#GROUP#${GROUP}g"`

	USER_BIN_PATH=`echo $USER_BIN_PATH|sed "s#USER#${USER}g
	s#GROUP#${GROUP}g
	s#USER_HOME_PATH#${USER_HOME_PATH}g"`

        VHOSTS_DIR=`echo $VHOSTS_DIR|sed "s#USER#${USER}g
	s#GROUP#${GROUP}g
        s#REMAINING_DOMAIN#${REMAINING_DOMAIN}g
        s#VHOST#${VHOST}g
        s#USER_HOME_PATH#${USER_HOME_PATH}g
	s#DOMAIN#${DOMAIN}g"`

        VHOST_DIR=`echo $VHOST_DIR|sed "s#USER#${USER}g
        s#GROUP#${GROUP}g
        s#REMAINING_DOMAIN#${REMAINING_DOMAIN}g
        s#VHOST#${VHOST}g
        s#DOMAIN#${DOMAIN}g
        s#USER_HOME_PATH#${USER_HOME_PATH}g
        s#VHOSTS_DIR#${VHOSTS_DIR}g"`

}
